﻿#region HeaderComments
// ***********************************************************************************************************************************************
// Copyright (C) # YEAR # # York #
// 作    者：# CaiTao #
// 创建日期：# CREATIONDATE #
// 功能描述：# UI移动动画组件 #
// 修改记录：# MODIFICATIONRECORD #
// ***********************************************************************************************************************************************
#endregion

using DG.Tweening;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace AnimationComponent
{
    public class SizeAnimatorControl : AnimationBase
    {
        #region  Editor

        [HorizontalGroup("startSize")]
        [HideInPlayMode]
        [PropertyOrder(-100)]
        public Vector2 startSize;   //动画开始位置

        [Button("StartSize"), GUIColor(0, 1, 0, 0.8F)]
        [HorizontalGroup("startSize", 80)]
        [HideInPlayMode]
        private void GetStartValue()
        {
            startSize = (transform as RectTransform).sizeDelta;
        }

        [HorizontalGroup("endSize")]
        [HideInPlayMode]
        [PropertyOrder(-100)]
        public Vector2 endSize;

        [Button("EndSize"), GUIColor(0, 1, 0, 0.8F)]
        [HorizontalGroup("endSize", 80)]
        [HideInPlayMode]
        private void GetEndValue()
        {
            endSize = (transform as RectTransform).sizeDelta;
        }

        [Button, GUIColor(0, 1, 0, 0.8F)]
        [HideInPlayMode]
        [PropertyOrder(-98)]
        private void ResetStartPosition()
        {
            (transform as RectTransform).sizeDelta = startSize;
        }

        [Button, GUIColor(0, 1, 0, 0.8F)]
        private void PlayAnimation_Editor()
        {
            PlayAnimation();
        }

        [Button, GUIColor(0, 1, 0, 0.8F)]
        private void PlayForward_Editor()
        {
            PlayForward();
        }

        [Button, GUIColor(0, 1, 0, 0.8F)]
        private void PlayBackward_Editor()
        {
            PlayBackwards();
        }

        #endregion

        public override void CreateAnimation()
        {
            tween = DOTween.To(() => startSize, (value) => {

                (transform as RectTransform).sizeDelta = value;

            }, endSize, animatorTime).SetAutoKill(false).Pause().SetLoops(1).SetEase(ease);

            tween.onPlay += () =>
            {
                OnAnimationPlayEvent();
                onPlay?.Invoke();
            };

            tween.onComplete += () =>
            {
                onComplete?.Invoke();
            };

            tween.onStepComplete+=()=>{
                OnAnimationStepCompentEvent();
                onStepComplete?.Invoke();
            };

            tween.onKill += () =>
            {
                onKill?.Invoke();
            };
        }


        /// <summary>
        /// 动画开始事件
        /// </summary>
        /// <param name="id"></param>
        private void OnAnimationPlayEvent()
        {
            aniPrePanelState = panelState;
            panelState = PanelsState.ANIMATIONING;
        }

        /// <summary>
        /// 动画完成事件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        private void OnAnimationStepCompentEvent()
        {
            switch (aniPrePanelState)
            {
                case PanelsState.STATUS1:
                    panelState = PanelsState.STATUS2;
                    break;
                case PanelsState.STATUS2:
                    panelState = PanelsState.STATUS1;
                    break;
            }
        }
    }
}

